package com.example.love_margin.Service.Impl;


import com.example.love_margin.Entity.Adopt;
import com.example.love_margin.Entity.Pet;
import com.example.love_margin.Entity.Until.PageInfo;
import com.example.love_margin.Mapper.AdoptMapper;
import com.example.love_margin.Mapper.PetMapper;
import com.example.love_margin.Service.AdoptService;
import com.example.love_margin.Service.Impl.Until.delPetsUtil;
import com.example.love_margin.Service.PetService;
import com.example.love_margin.Untils.ResultCode;
import com.example.love_margin.Untils.forDelete;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class AdoptServiceImpl implements AdoptService {

    @Autowired
    AdoptMapper adoptMapper;
    //批量删除实例化
    @Autowired
    delPetsUtil delP = new delPetsUtil();

    //创建删除批量文件的实例化
    @Autowired
    forDelete delete = new forDelete();

    //宠物接口删除以领养宠物的实例化
    @Autowired
    PetService petService;

    @Autowired
    PetMapper petMapper;

    @Override
    public Object addAdopt(Adopt adopt) {
        adopt.setAdopt_state("unPass");
        if(adoptMapper.addAdopt(adopt)==1){
            return ResultCode.SUCCESS.result("提交成功");
        }
        return ResultCode.SUCCESS.result("提交失败");

    }

    @Override
    public Object deleteAdopt(int adopt_id) {
        if(adoptMapper.deleteAdopt(adopt_id)==1){
            return ResultCode.SUCCESS.result("删除成功");
        }
        return ResultCode.SUCCESS.result("删除失败");

    }

    @Override
    public Object delAdoptMany(Integer[] adopt_ids) {
        String sql = delP.deleSQL("adopt","adopt_id",adopt_ids);
        if(adoptMapper.deleteScienceM(sql)>=1){
            return ResultCode.SUCCESS.result( "删除成功");
        }else {
            return ResultCode.SUCCESS.result( "删除失败");
        }
    }

    @Override
    public Object updateAdopt(Adopt adopt) {
        if(adoptMapper.upSale(adopt)==1){
            return ResultCode.SUCCESS.result("修改成功");
        }
        return ResultCode.SUCCESS.result("修改失败");

    }

    @Override
    public Object updateAdoptState(int adopt_id) {
        Adopt adopt = adoptMapper.selectAdoptO(adopt_id);
        if(adoptMapper.upOneStat(adopt_id)==1){
            try {
                petService.deletePet(adopt.getPet_id());
            }catch (Exception e){
                System.out.println(e);
            }
            return ResultCode.SUCCESS.result("通过成功");
        }
        return ResultCode.SUCCESS.result("通过失败");

    }

    //领养通过后改状态码，并删除宠物
    @Override
    public Object updateAdoptStates(Integer[] adopt_ids) {
        String sql = delP.upOneSQL("adopt","adopt_state","Pass","adopt_id",adopt_ids);
        if(adoptMapper.upStateMany(sql)>=1){
            String ss = delP.admin("pet_id","adopt","adopt_id",adopt_ids);
            try {
                List<Integer> pets = adoptMapper.selectPetAll(ss);
                //批量删除的数组转换
                Integer[] Pets = (Integer[]) pets.toArray();
                petService.delPets(Pets);
            }catch (Exception e){
                System.out.println(e);
            }

            return ResultCode.SUCCESS.result("通过成功");
        }
        return ResultCode.SUCCESS.result("通过失败");
    }

    @Override
    public Object selectAdoptOne(int adopt_id) {
        return ResultCode.SUCCESS.result(adoptMapper.selectAdoptO(adopt_id));
    }

    @Override
    public Object selectMyAdopt(int adopt_id,String adopt_state) {

       Integer[] pet_ids = adoptMapper.selectOneAdoptAll(adopt_id, adopt_state);
        System.out.println(Arrays.toString(pet_ids));
       String sql = delP.admin("*","pet","pet_id",pet_ids);
       List<Pet> pets = petMapper.selectPetAllM(sql);
        return ResultCode.SUCCESS.result(pets);

    }

    @Override
    public Object selectAdoptState(Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Adopt> JJ = adoptMapper.selectAdoptM();
        // 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
        PageInfo<Adopt> pageInfo = new PageInfo<>(JJ);
        return ResultCode.SUCCESS.result(pageInfo);
    }

    @Override
    public Object selectAdoptPage(String adopt_state) {
        return null;
    }
}
